﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace Contracts.Library
{
    public class Join<T0, T1>
    {
        private T0 in0;
        private T1 in1;
        private readonly HashSet<int> presentValues;

        public Join()
        {
            presentValues = new HashSet<int>();
            Reset();            
        }

        public void InputA(T0 input)
        {
            in0 = input;
            presentValues.Add(0);
            JoinIfComplete();
        }

        public void InputB(T1 input)
        {
            in1 = input;
            presentValues.Add(1);
            JoinIfComplete();
        }

        private void JoinIfComplete()
        {
            if (presentValues.Count == 2)
            {
                this.OnCompleted(in0, in1);
                Reset();
            }
        }

        private void Reset()
        {
            presentValues.Clear();
        }

        public event Action<T0, T1> OnCompleted;
    }
}
